package April._25.homework.work5;

import untils.TestFlag;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class ClientTask implements Runnable{
    Socket socket;
    public ClientTask(Socket socket) {
        this.socket = socket;
    }
    @Override
    public void run() {
        boolean flag = true;
        Scanner scanner = new Scanner(System.in);
        while (flag) {
            String msgSend = scanner.nextLine();
            if(TestFlag.testFlag){
                System.out.println("任务已提交");
            }
            try {
                //获取输出流socket
                OutputStream outputStream = socket.getOutputStream();
                //装换为字符流
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
                if(TestFlag.testFlag){
                    System.out.println("开始发送");
                }
                outputStreamWriter.write(msgSend);
                outputStreamWriter.flush();
                if(TestFlag.testFlag){
                    System.out.println("发送完成、准备接收");
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            String msg=null;
            try {
                //获取输入流socket
                InputStream inputStream = socket.getInputStream();
                //转换为字符流
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                //读取消息
                char[] chars = new char[1024];
                int read = inputStreamReader.read(chars);
                msg=new String(chars,0,read);
                System.out.println("msg = " + msg);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
